<?php
if(!defined('IN_ADMIN')) {
	exit('Acess Denied');
}

if(submitcheck('xmlpost')) {
	if(!empty($_POST['clientids'])){
		foreach($_POST['clientids'] as $id=>$clientid) {
			$key = $_POST['clientkeys'][$id];
			DB::query("UPDATE ".DB::table('xml_client')." SET `clientid`='$clientid', `key`='$key' WHERE id=$id");
		}
	}
	

	if(!empty($_POST['newclientid'])) {
		foreach($_POST['newclientid'] as $key=>$value) {
			$clientkey = $_POST['newclientkey'][$key];
			$insertdata[] = "('$value', '$clientkey')"; 
		}
		$data = implode(",", $insertdata);
		DB::query("INSERT INTO ".DB::table('xml_client')." (`clientid`, `key`) VALUES $data");
	}
	cpmsg('update_success', '?action=xmlpost');
}
if($_GET['op'] == 'del' && !empty($_GET['id'])) {
	$id = intval($_GET['id']);
	DB::query('DELETE FROM '.DB::table('xml_client')." WHERE id='".$id."';");
	cpmsg('update_success', '?action=xmlpost');
}

shownav('global', 'nav_xmlpost');
showsubmenu('nav_xmlpost');
showformheader('xmlpost');
showtableheader('');
showsubtitle(array('display_order', 'clientid', 'clientkey', ''));

$query = DB::query("SELECT * FROM ".DB::table('xml_client'));
while($value = DB::fetch($query)) {
	$rowitem = array();
	$rowitem[] = '<input name="ids['.$value['id'].']" type="text" size="2" value="'.$value['id'].'" />';
	$rowitem[] = '<input name="clientids['.$value['id'].']" type="text" size="8" value="'.$value['clientid'].'" />';
	$rowitem[] = '<input name="clientkeys['.$value['id'].']" type="text" size="8" value="'.$value['key'].'" />';
	$rowitem[] = '[<a href="?action=xmlpost&op=del&id='.$value['id'].'">'.lang('nav_del').'</a>]';
	showtablerow('', array(), $rowitem);
}

echo "
<script type=\"text/JavaScript\">
	var rowtypedata = [
		[ 
			[1, '', ''], 
			[1, '<input name=\"newclientid[]\"  value=\"\"  type=\"text\"  class=\"txt\">', '',''],
			[1, '<input name=\"newclientkey[]\"  value=\"\"  type=\"text\"  class=\"txt\">', '', ''],
			
		],
	];
</script>";
echo '<tr><td colspan="8"><div><a href="###" onclick="addrow(this,0)" class="addtr">'.$lang['client_add'].'</a></div></td></tr>';

showsubmit('xmlpost');
showtablefooter();
showformfooter();
bind_ajax_form();
?>
